﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Win32;
using System.IO;

namespace ConvertImage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region member variable
        List<string> _selectImages = new List<string>();
        #endregion

        #region constructor
        public MainWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region event handle
        private void btnSourceImages_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.CheckFileExists = true;
            fileDialog.CheckPathExists = true;
            fileDialog.Filter = "image files|*.bmp;*.png;*.jpg;*.gif|all type files|*.*";
            fileDialog.Multiselect = true;
            if (fileDialog.ShowDialog() == true)
            {
                _selectImages = fileDialog.FileNames.ToList();
                lvImages.ItemsSource = _selectImages;
                try
                {
                    var border = VisualTreeHelper.GetChild(lvImages, 0) as Border;
                    var scrollviewer = VisualTreeHelper.GetChild(border, 0) as ScrollViewer;
                    scrollviewer.ScrollToRightEnd();
                    scrollviewer.ScrollToHome();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            if (_selectImages.FirstOrDefault() == null) return;
            DirectoryInfo newDirectory;
            try
            {
                string path = _selectImages.First();
                DirectoryInfo parentDirectory = Directory.GetParent(path);
                string newPath = "newImage" + DateTime.Now.ToString("hhMMssfff");
                newDirectory = parentDirectory.CreateSubdirectory(newPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            string extension = cbFormat.Text;
            if (!extension.StartsWith("."))
                extension = "." + extension;

            int width = 0;
            int.TryParse(cbWidth.Text, out width);

            int height = 0;
            int.TryParse(cbHeight.Text, out height);

            using (ThumbnailsHelper thumbHelper = new ThumbnailsHelper())
            {
                if (width != 0)
                    thumbHelper.Width = width;

                if (height != 0)
                    thumbHelper.Width = height;

                foreach (string str in _selectImages)
                {
                    try
                    {
                        string newFilePath = newDirectory.FullName + @"\" + Path.GetFileNameWithoutExtension(str) + extension;
                        thumbHelper.GenerateFromPath(str, newFilePath, extension);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        continue;
                    }
                }
            }

            System.Diagnostics.Process.Start(newDirectory.FullName);
        }
        #endregion
    }
}
